package com.toman.Adpter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Entity.Msg_Entity;
import com.toman.Interfaces.BaseUrl;
import com.toman.MyApplication;
import com.toman.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Request;
import okhttp3.Response;

/**
 * 普通的 ExpandableListView 的适配器
 */
public class MsgExpandableListAdapter extends BaseExpandableListAdapter {

    private static final String TAG = "test";
    ArrayList<String> arrayList;
    ArrayList<ArrayList<Msg_Entity>> array;

    // private String[] groupData;
    // private String[][] childData;
    ImageView mNor_Delimg;

    private onGroupExpandedListener mOnGroupExpandedListener;
    Context context;
    MyApplication myApplication;


    public MsgExpandableListAdapter(Context context, ArrayList<String> arrayList, ArrayList<ArrayList<Msg_Entity>> array, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.array = array;
        this.myApplication = myApplication;
    }

    public void setOnGroupExpandedListener(onGroupExpandedListener onGroupExpandedListener) {
        mOnGroupExpandedListener = onGroupExpandedListener;
    }

    public void update(ArrayList<ArrayList<Msg_Entity>> array) {
        this.array = array;
        notifyDataSetChanged();

    }
    @Override
    public int getGroupCount() {
        return arrayList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        try {
            return array.get(groupPosition).size();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return arrayList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return array.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.mant_item_msg, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.mMantTitle);
            groupViewHolder.img = (ImageView) convertView.findViewById(R.id.mMantImg);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(arrayList.get(groupPosition));

        if (isExpanded) {
            groupViewHolder.img.setBackgroundResource(R.mipmap.xia);
        } else {
            groupViewHolder.img.setBackgroundResource(R.mipmap.you);
        }

        return convertView;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        //   if (convertView == null) {
        childViewHolder = new ChildViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.mant_items_msg1, parent, false);
        childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
        childViewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
        childViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);

        mNor_Delimg = (ImageView) convertView.findViewById(R.id.mMantChidImg);
        ImageView mAvatar = (ImageView) convertView.findViewById(R.id.mAvatar);
        if (array.get(groupPosition).get(childPosition).getType().equals("1")) {
            mAvatar.setImageResource(R.mipmap.msg_icon);
        } else {
            mAvatar.setImageResource(R.mipmap.rubbish);

        }

//            convertView.setTag(childViewHolder);
//        } else {
//            childViewHolder = (ChildViewHolder) convertView.getTag();
//        }
//        if(array.get(groupPosition).get(childPosition).getImg().equals("")){
//            Picasso.with(context).load(R.mipmap.avatar_default_login).into(mAvatar);
//        }else{
//            Picasso.with(context).load(array.get(groupPosition).get(childPosition).getImg()).into(mAvatar);
//        }
        childViewHolder.tvTitle.setText(array.get(groupPosition).get(childPosition).getTitle());
        childViewHolder.tvContent.setText(array.get(groupPosition).get(childPosition).getContent());
        childViewHolder.tvTime.setText(array.get(groupPosition).get(childPosition).getTime_start() + "~" + array.get(groupPosition).get(childPosition).getTime_end());

        if (array.get(groupPosition).get(childPosition).isIsok()) {
            mNor_Delimg.setVisibility(View.VISIBLE);
        } else {
            mNor_Delimg.setVisibility(View.INVISIBLE);
        }

        mNor_Delimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelDialog(array.get(groupPosition).get(childPosition).getId(), groupPosition, childPosition);
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        Log.d(TAG, "onGroupExpanded() called with: groupPosition = [" + groupPosition + "]");
        if (mOnGroupExpandedListener != null) {
            mOnGroupExpandedListener.onGroupExpanded(groupPosition);
        }
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        Log.d(TAG, "onGroupCollapsed() called with: groupPosition = [" + groupPosition + "]");
    }

    private static class GroupViewHolder {
        TextView tvTitle;

        ImageView img;

    }

    private static class ChildViewHolder {
        TextView tvTitle;
        TextView tvContent;
        TextView tvTime;
        ImageView imgIcon;
    }

    /*
       提示删除
    */
    public void DelDialog(final Integer uid, final int ps, final int po) {
        final String token = myApplication.getSp().getString("tokenId", "");
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.del_layout3);
        TextView textView = (TextView) dialog.getWindow().findViewById(R.id.tv_msg);
        textView.setText("你确定要删除消息吗");
        dialog.getWindow().findViewById(R.id.mDelUserDIomss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.getWindow().findViewById(R.id.mDelUser).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpUtils
                        .post(BaseUrl.msg_del)
                        .params("token", token)
                        .params("id", uid.toString())
                        .execute(new StringCallback() {
                            @Override
                            public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                                try {
                                    JSONObject js = new JSONObject(result);
                                    int code = js.getInt("code");
                                    if (code == 1) {
                                        Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                        ArrayList<Msg_Entity> s = array.get(ps);
                                        s.remove(po);
                                        notifyDataSetChanged();
                                        dialog.dismiss();
                                    } else {
                                        Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
            }
        });
    }


}
